//
//  CYRotationLoopProgressView.m
//  maskProgressDemo
//
//  Created by Mr.GCY on 2017/6/2.
//  Copyright © 2017年 Mr.GCY. All rights reserved.
//

#import "XYRotationLoopProgressView.h"
@interface XYRotationLoopProgressView ()
{
    CGFloat _angleInterval;
}
@end
@implementation XYRotationLoopProgressView
// 加载时显示的文字
NSString * const SDRotationLoopProgressViewWaitingText = @"LOADING...";

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(changeAngle) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    }
    return self;
}

- (void)changeAngle
{
    _angleInterval += M_PI * 0.08;
    if (_angleInterval >= M_PI * 2) _angleInterval = 0;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    if (self.imageName) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        //保存初始状态（压栈操作，保存一份当前图形上下文）
        CGContextSaveGState(context);
        UIImage *image = [UIImage imageNamed:self.imageName];
        //需要绘制的图片
        CGRect rectImage = CGRectMake(0.0, 0.0, rect.size.width, (rect.size.width*image.size.height/image.size.width));
        
        //旋转
        //修改旋转中心点
        CGContextTranslateCTM(context, rectImage.size.width * 0.5, rectImage.size.height * 0.5);
        CGContextRotateCTM(context, _angleInterval);
        //修复图片旋转位置点
        CGContextTranslateCTM(context, -rectImage.size.width * 0.5, -rectImage.size.height * 0.5);
        
        //在rect范围内完整显示图片-正常使用
        [image drawInRect:rectImage];
        //恢复到初始状态（出栈操作，恢复一份当前图形上下文）
        CGContextRestoreGState(context);
    }else{
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGFloat xCenter = rect.size.width * 0.5;
        CGFloat yCenter = rect.size.height * 0.5;
        [[UIColor lightGrayColor] set];
    
        CGContextSetLineWidth(ctx, 4);
        //设置缺口大小 通过- M_PI * 0.06
        CGFloat to = - M_PI * 0.7 + _angleInterval; // 初始值0.05
        CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - 10;
        CGContextAddArc(ctx, xCenter, yCenter, radius, _angleInterval, to, 0);
        //画线
        CGContextStrokePath(ctx);
    
        // 加载时显示的文字
        if (self.isShowText) {
            NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
            attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:13];
            attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
            [self setCenterProgressText:SDRotationLoopProgressViewWaitingText withAttributes:attributes];
        }
    }
}
@end
